<template>
  <div class="dashboard-container">
    <header>
      <el-form :inline="true" :model="searchFrom" class="demo-form-inline">
        <el-form-item label="名称">
          <el-input
            v-model="searchFrom.name"
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </header>
    <main>
      <div style="display: flex; justify-content: flex-end; margin-right: 65px">
        <el-button type="success" @click="toAdd()">新增</el-button>
      </div>

      <el-table :data="tableData">
        <el-table-column prop="id" label="序号" width="180"> </el-table-column>
        <el-table-column prop="name" label="名称"> </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="toDelete(scope.row)" type="danger" size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </main>
    <el-dialog title="新增树种" :visible="visible" width="30%">
      <el-input v-model="treeName" placeholder="请输入树种名"></el-input>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="addTreeName()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { createList, deleteType, getTypeList } from "@/api/type";
export default {
  name: "Dashboard",
  computed: {
    ...mapGetters(["name"]),
  },
  components: {},
  data() {
    return {
      visible: false,
      treeName: "",
      searchFrom: {
        currentPage: 1,
        name: "",
        pageSize: 100,
      },
      tableData: [],
    };
  },
  mounted() {
    this.getTypeList();
  },
  methods: {
    getTypeList() {
      getTypeList(this.searchFrom).then((res) => {
        console.log(res);
        this.tableData = res.data;
      });
    },
    toAdd() {
      this.visible = true;
    },
    addTreeName() {
      createList({ name: this.treeName }).then((res) => {
        console.log(res);
        this.visible = false;
        this.getTypeList();
      });
    },
    toDelete(row) {
      this.deleteID = row.id;
      let that = this;
      console.log(this.deleteID);
      this.$confirm("是否要将该数据删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteType({ id: that.deleteID }).then((res) => {
            console.log(res);
            that.getTypeList();
            this.$message({
              type: "success",
              message: "删除成功!",
            });
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    cancel() {
      this.visible = false;
    },
    onSubmit() {
      console.log(this.searchFrom);
      this.getTypeList();
    },
  },
};
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
